var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];



//运营经理关联
function myFunctiopowerButtonn(){
    //销售经理

    var manager=$("#organization_name").val();
    var user_data = {
        "pageNum":1,

        "pageSize":20,

    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if(data.errorCode=="0"){
            var jsonObj =data.group;
            if(jsonObj==""){
                $("#tuan").html("<li>无数据</li> ");
            }else {
                console.log(jsonObj)

                var optionstring = "";
                for (var  i= 0; i < jsonObj.length; i++) {
                    optionstring += "<li onclick='relation("+ jsonObj[i].id+",\""+jsonObj[i].organization_name+"\")' data-align=\"" + jsonObj[i].id + "\" > " + jsonObj[i].organization_name + "</li>";
                }
                $("#tuan").html(optionstring);
                console.log(jsonObj)
            }
        }else{
            $("#tuan").html("<li>无数据</li> ");
        }
    }
    /*root.member.getGroup*/
    web.doAjax(root.member.getGroup,'post',user_data, succCallBack);

}
//取光标
$("#organization_name").focus(function () {
    $("#tuan").show()
});
$(document).bind("click",function(e){
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if($(e.target).closest("#tuan").length == 0 && $(e.target).closest("#organization_name").length == 0){
        //点击id为menu之外且id不是不是open，则触发
        $("#tuan").hide()
    }
})
function relation(id,ce) {
    $("#organization_name").val(ce).text(id)
    $("#tuan").hide()
    console.log($("#organization_name").text())
}




function searchList() {
    $.ajax({
        /*root.member.custAccountList*/
        url: root.member.queryGroundMember,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":1,
            "pageSize":50,

            "groundPushPersonName":$("#groundPushPersonName").val(),
            "loginAccount": $("#loginAccount").val(),
            "mobilePhone": $("#mobilePhone").val(),
            "registerPlatform":$("#registerPlatform").val(),
            "beginTime":$("#beginTime").val(),
            "endTime":$("#endTime").val()

        }),
        success: function (data) {
            console.log(data);
            // couponlist(data.result);
            list(data.list, 0);
            $('.M-box3').pagination({
                pageCount: data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    console.log(api)
                    $.ajax({
                        /*root.member.custAccountList*/
                        url: root.member.queryGroundMember,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":50,



                            "groundPushPersonName":$("#groundPushPersonName").val(),
                            "loginAccount": $("#loginAccount").val(),
                            "mobilePhone": $("#mobilePhone").val(),
                            "registerPlatform":$("#registerPlatform").val(),
                            "beginTime":$("#beginTime").val(),
                            "endTime":$("#endTime").val()

                        }),
                        success: function (result) {
                            console.log(result)
                            list(result.list);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}

function list(data, index){
    sessionStorage.setItem("custInfoRegisterFormList", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组
    if(data !=null && data != ''){
        var datashu =data[0].rowNo;
    $.each(data, function (i, field) {
        var rowData = new Array();
        var datashus = datashu++
        rowData.push(field.rowNo);
        rowData.push(field.loginAccount);
        rowData.push(field.mobilePhone);
        rowData.push(field.registerPlatform);

        var registerTime=field.registerTime;
        var registerTimeIndex=registerTime.indexOf(".0");
        registerTime=registerTime.substr(0,registerTimeIndex);
        rowData.push(registerTime);

        rowData.push(field.groundPushPersonName);

        var updateTime=field.updateTime;
        var updateTimeIndex=updateTime.indexOf(".0");
        updateTime=updateTime.substr(0,updateTimeIndex);
        rowData.push(updateTime);

        //rowData.push(field.updateTime);
        rowData.push(datashus);
        rowData.push(field.memberId);//会员id
        dataSource.push(rowData);
    });
    }else{
    }
    initDataTable(dataSource);
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';//10:Pc;20:安卓;30:ios;40:H5
                        if(row[3]==10){
                            val='PC'
                        }
                        else if(row[3]==20){
                            val='安卓'
                        }
                        else if(row[3]==30){
                            val='IOS'
                        }
                        else if(row[3]==40){
                            val='H5'
                        }
                        return val;
                    },
                    "targets":3
                },

                {
                    "render": function (data, type, row, meta) {
                        var val='';
                            val = '<div class="action" id="action'+row[3]+'">' +

                                '<a class="powerButton 181">' +
                                '<span class="list"  ' +
                                'onclick="detail(\'会员\',\'custInfoRegisterForm_edit.html\',\''
                                +row[8]+'\')">编辑' +
                                '</span>' +
                                '</a>' +
                                '</div>';
                        return val;
                    },
                    "targets":7
                }
            ]
        });
//权限控制函数
        power()
    };
}
//关闭弹框
var index=0;
function confirm(id,val) {
    var curInd = layer.confirm('<span id="status" style="width:200px;height:100px;resize: none;" maxlength="200" >您确定要关闭此账号吗？</span>', {title:'提示',btn: ['确定', '取消']}, function(index){

        finish1(id,curInd,val)
        layer.close(curInd);
    },function(){
        layer.close(curInd);

    });
}
//第一个
function finish1(id,val){
    var target = document.getElementById(''+id+'');
    target.parentElement.parentElement.parentElement.previousElementSibling.innerHTML='关闭';
    console.log(target)

    var reason=$('#reason'+id+'').val();
    console.log(reason)
    if(reason!=''){
        $.ajax({
            contentType: "application/json;charset=UTF-8",
            /*root.member.updateMemberStatus*/
            url:root.member.updateGroundPushPerson,
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            data: JSON.stringify({
                "inviteCode":id,
                "status":20,//关闭
                optUserId:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
                optUserName:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name

            }),
            success: function (data) {
                console.log(data)

                target.onclick = null;
                target.setAttribute("onclick", 'confirm('+id+')')
                target.parentElement.parentElement.parentElement.previousElementSibling.innerHTML='关闭'
                searchList();

            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        })}
    else{
        $.Huimodalalert('处理结果不能为空！',2000);
    };
    layer.close(index);
}

//开启弹框
var index=0;
function confirm2(id,val) {
    var curInd = layer.confirm('<span id="status" style="width:200px;height:100px;resize: none;" maxlength="200" >您确定要开启此账号吗？</span>', {title:'提示',btn: ['确定', '取消']}, function(index){

        finish2(id,curInd,val)
        layer.close(curInd);
    },function(){
        layer.close(curInd);

    });
}
//第一个
function finish2(id,val){
    var target = document.getElementById(''+id+'');

    target.parentElement.parentElement.parentElement.previousElementSibling.innerHTML='开启';

    var reason=$('#reason'+id+'').val();
    if(reason!=''){
        $.ajax({
            contentType: "application/json;charset=UTF-8",
            /*root.member.updateMemberStatus*/
            url:root.member.updateGroundPushPerson,
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            data: JSON.stringify({
                "inviteCode":id,
                "status":10,//开启
                optUserId:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
                optUserName:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name

            }),
            success: function (data) {

                target.onclick = null;
                target.setAttribute("onclick", 'confirm('+id+')')
                searchList();
            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        })}
    else{
        $.Huimodalalert('处理结果不能为空！',2000);
    };
    layer.close(index);
}
//查看详情
function detail(title,url,member_id) {
    //alert("member_id000:"+member_id);
    sessionStorage.setItem("memberId",member_id);

    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}




